package com.joymart.admin.rest;


import com.joymart.common.utils.SecurityUtils;
import com.joymart.protocol.model.Protocol;
import com.joymart.protocol.service.ProtocolService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

@RestController
public class ProtocolAdminController {


    @Autowired
    private ProtocolService protocolService;

    @GetMapping("/admin/protocols")
    public Page<Protocol> protocols(
            @RequestParam(value = "type", defaultValue = "")String type,
            @RequestParam(value = "pageNo", defaultValue = "0")Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "20")Integer pageSize
    ) {
        return protocolService.searchProtocols(type, PageRequest.of(pageNo, pageSize));
    }

    @PutMapping("/admin/protocols")
    public void updateProtocols( @RequestBody ProtocolUpdateParams params) {
        SecurityUtils.requiresRestAuthority("SystemSettings");
        //协议只能迭代版本，不能修改已经发布的协议
        protocolService.create(params.getType(), params.getTitle(), params.getContent());
    }

    @Data
    public static class ProtocolUpdateParams {
        private String type;
        private String title;
        private String content;
    }


}
